package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.BspsMchntSignInfoMapper;
import cc.rengu.igas.bsps.common.entity.MchntSignInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 商户签约信息表操作接口实现类
 * Created by 王成 on 2018/6/15.
 */
public class BspsMchntSignInfoMapperImpl implements BspsMchntSignInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public MchntSignInfo selectBspsMchntSignInfoById(String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntSignInfoById");
        MchntSignInfo mchntSignInfo = new MchntSignInfo();
        mchntSignInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntSignInfo, mchntSignInfo);
        if (0 != iReturnCode) {
            rglog.error("根据非业务主键<{}>获取商户签约信息失败,iReturnCode:<{}>", id, iReturnCode);
            return null;
        }
        return mchntSignInfo;
    }

    @Override
    public MchntSignInfo selectBspsMchntSignInfoByPrimaryKey(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntSignInfoByPrimaryKey");
        MchntSignInfo mchntSignInfo = new MchntSignInfo();
        mchntSignInfo.setInstId(instId);
        mchntSignInfo.setMchntNo(mchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntSignInfo, mchntSignInfo);
        if (0 != iReturnCode) {
            rglog.error("根据机构号：<{}>,商户号：<{}>获取商户签约信息失败,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return mchntSignInfo;
    }
}
